<!DOCTYPE HTML>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
    <title>我的购物车</title>
    <link href="css/cart.css" rel="stylesheet" type="text/css">
    <link rel="stylesheet" href="css/foot.css"/>
    <link rel="stylesheet" href="css/head.css"/>
    <meta http-equiv="Content-type" content="text/html; charset=UTF-8"/>
    <noscript>抱歉，你的浏览器不支持 JavaScript!</noscript>
    <script type="text/javascript" src="js/base-v1.js" charset="utf-8"></script>
    <script type="text/javascript" src="js/jquery-1.2.6.min.js" charset="utf-8"></script>
    <script type="text/javascript" src="js/cookie.js" charset="utf-8"></script>
    <script type="text/javascript" src="js/common.js" charset="utf-8"></script>
    <script type="text/javascript" src="js/jquery.cookie.js" charset="utf-8"></script>
    <script type="text/javascript" src="js/jquery.min.js" charset="utf-8"></script>
    <script type="text/javascript" src="js/jquery-extend.js"></script>
    <script type="text/javascript" src="js/lib-v1.js" charset="utf-8"></script>
    <link href="css/prodList.css" rel="stylesheet" type="text/css">
    <link rel="icon" type="image/x-icon" href="img/head/favicon.ico"/>
    <!--    <script type="text/javascript" src="js/logout.js"></script>-->
    <meta charset="UTF-8">
    <script type="text/javascript">
        window.onload= function () {
            var op= "";
            var info="";
            var  id=sessionStorage.getItem("name");
            if(id !=null){

                op+="      <font color='red'> 欢迎用户: "+id+" </font>\n" +
                    "  \ &nbsp;  || &nbsp;                    <a onclick=\"out()\">退出</a>&nbsp;&nbsp;&nbsp;&nbsp;"
                $("#head_span").html(op);
                query();
            }else{
                alert("当前未登录 ,请登陆");
                location.href="login.html";
                /*  info+="<a href=\"login.html\" id=\"login\"  >登录</a>\n " +
                      "&nbsp;  || &nbsp;           <a href=\"regist.html\">注册</a>"
                  $("#head_span").html(info);*/
            }


        }
        function  out(){
            sessionStorage.clear("name");
            location.href="index.html"
        }
        /**
         * 删除购物车
         * @param productId 商品id
         * @param userId 用户id
         */
        function del(coupon_id) {
            if (confirm("您确定删除吗？")) {
                $.ajax({
                    url: "coupon/deleteCoupon",
                    data: {"id":coupon_id},
                    dataType: "json",
                    type: "GET",
                    success: function (data) {
                    },
                    error: function () {
                    }
                });
            }
            location.reload();
        }

        /**
         * 修改商品数量
         * @param productId 商品id
         * @param userId 用户id
         */
        function inputNum(productId) {
            let num = document.getElementById(productId).value;
            let numOld = document.getElementById("hid_" + productId).value;
            if (num === "" || num == null) {
                alert("数量填写错误");
                return;
            }
            if (num === 0) {
                del(productId);
                return;
            }
            $.ajax({
                url: "cart/productNum",
                data:{"productId":productId},
                dataType: "json",
                type: "POST",
                success: function (data) {
                    let total = data.product_num;
                    if (num > total) {
                        alert("购买数量最大" + total);
                        document.getElementById(productId).value = numOld;
                        return;
                    }
                    $.ajax({
                        url: "cart/updNum",
                        data:{"productId":productId,"num":num},
                        dataType: "json",
                        type: "GET",
                        success: function (data) {

                        },
                    });
                },
            });
            location.reload();
        }

        /**
         *  查询用户优惠券
         */
        function query() {
            $.ajax({
                url: "coupon/queryCouponById",
                dataType: "json",
                type: "GET",
                success: function (data) {
                    if (data.length > 0) {
                        $.each(data, function (index, cart) {
                            let coupon_id = cart.coupon_id;

                            let coupon_describe = cart.coupon_describe;
                            let coupon_type = cart.coupon_type;
                            let coupon_state = cart.coupon_state;
                            let coupon_startDate = cart.coupon_startDate;
                            let coupon_endDate = cart.coupon_endDate;
                            let coupon_discount = cart.coupon_discount;
                            let couponTypeName = cart.couponTypeName;
                            $("#items").append("<tr class='prods'>" +
                                "<td style='width:100px;'><input name='coupon_id' id='myTable'  value='" + coupon_id + "' type='checkbox'  checked='checked' class='select_items' style='padding: 0;margin: 2px;' onchange='checkChanged(this)' /><a target='_blank' style='padding:0;margin: 0;'></a></td>" +
                                "<td ><input type='hidden' name='coupon_id' value='"+coupon_id+"'/><a target='_blank' id='prods_name'>"+coupon_describe+"</a></td>" +
                                "<td >"+couponTypeName+"</td>" +
                                "<td >"+coupon_startDate+"</td>" +
                                "<td >"+coupon_endDate+"</td>" +
                                "<td >"+coupon_discount+"</td>" +
                                "<td ><a href='javascript:void(0)' onclick='del(\""+coupon_id + "\")'>删除</a></td>" +
                                "</tr>");
                        })
                    } else {
                        alert("您还没有优惠券");
                    }
                }
            });
        }


        /**
         * 搜索
         *
         */
        function search(a) {
            let query = document.getElementById(a).value;
            window.location.href = "./search.html?query=" + query;
        }

        /**
         * 全选改变
         * @param element DOM
         */
        function changeSelect(element){
            let selected = element.checked;
            $(".select_items").prop("checked", selected);
            checkChanged(null);
        }

        /**
         * 更改勾选后修改页面
         */
        function checkChanged(element){
            let money = 0;
            for (let i = 0; i < $(".prods").length; i++) {
                let prod = $(".prods").eq(i);
                let checked = prod.find("input[name=prodC]").prop("checked");
                if (checked){
                    // 如果是勾选状态
                    let num = parseInt(prod.find(".buyNumInp1").val());
                    let price = parseFloat(prod.find("#prods_price").text());
                    money = money + num * price;
                }
            }
            $("#span_2").text(money + "￥");
            if (money === 0){
                $("#total #total_2 a").attr("disabled", true);
                $("#total #total_2 a").css("background-color", "lightgrey");
                $("#total #total_2 a").hover(
                    function(){$("#total #total_2 a").css("background-color", "lightgrey");}
                )
            }else {
                $("#total #total_2 a").attr("disabled", false);
                $("#total #total_2 a").css("background-color", "#D9141E");
                $("#total #total_2 a").hover(
                    function(){$("#total #total_2 a").css("background-color", "#DD4343")},
                    function(){$("#total #total_2 a").css("background-color", "#D9141E")},
                )
            }
        }

        /**
         * 删除选中项
         * @param element 标签
         */
        function deleteSelected(element){
            for (let i = 0; i < $(".prods").length; i++) {
                let prod = $(".prods").eq(i);
                let checked = prod.find("input[name=prodC]").prop("checked");
                if (checked){
                    // 如果是勾选状态
                    let productId = prod.find("input[name=productId]").val();
                    del(productId);
                }
            }
        }
    </script>
</head>

<body >
<div id="common_head">
    <div class="line1">
        <div class="content">
            <ul>
                <li class="fore1" id="loginbar" clstag="homepage|keycount|home2013|01b">
                    <span id="head_span">


                          <a href="login.html" id="login" >登录</a>&nbsp;&nbsp;|&nbsp;&nbsp;
                        <a href="regist.html">注册</a>


                    </span>
                </li>
            </ul>
        </div>
    </div>
    <div class="line2">
        <a href="myself.html">
            <img id="erwm" src="img/head/e2.png" alt="e2" width="76"  />
        </a>
        <input type="text" value="" accesskey="s" id="key" autocomplete="off"
               onkeydown="if(event.keyCode===13) search('key');"/>
        <input type="button" value="搜 索" onclick="search('key')"/>
        <span id="goto">
			<a id="goto_myOrder" href="./myorder.html">我的订单</a>
			<a id="goto_cart" href="./mycart.html">我的购物车</a>
            <a id="goto_coupon" href="./coupon.html">优惠券中心</a>
		</span>
        <img id="erwm" src="img/head/e2.png" alt="e2" width="76"/>
    </div>
    <div class="line3">
        <div class="content">
            <ul>
                <li><a href="./index.html">首页</a></li>
                <li><a href="quanbu.html">全部商品</a></li>
                <li><a href="./instant-buy.html">秒杀商品</a></li>
                <li><a href="javascript:void(0)">电脑平板</a></li>
                <li><a href="javascript:void(0)">家用电器</a></li>
                <li><a href="javascript:void(0)">汽车用品</a></li>
                <li><a href="javascript:void(0)">食品饮料</a></li>
                <li><a href="javascript:void(0)">图书杂志</a></li>
                <li><a href="javascript:void(0)">服装服饰</a></li>
                <li><a href="javascript:void(0)">大英理财</a></li>
            </ul>
        </div>
    </div>
</div>

<div class="warp">
    <table id="items">
        <tr>
            <th style="width: 90px"><input name="all" type="checkbox" checked="checked" onclick="changeSelect(this)" id="checkbox1"/><label
                    for="checkbox1">全选</label></th>
            <th>优惠券信息</th>
            <th>优惠券类型</th>
            <th>领取时间</th>
            <th>过期时间</th>
            <th>折扣</th>
            <th>操作</th>
        </tr>
    </table>
</div>

<div class="fixed">
    <a href="https://github.com/GoogleLLP/SuperMarket" target="_blank">
        <img src="./img/head/qr.jpg" width="100" alt="qrcode"/>
    </a>
    <a href="#common_head" class="toTop"></a>
</div>
<div id="common_foot">
    <p>
        Copyright © 2011-2019 网上商城 版权所有 保留一切权利 粤CM-20201119 | 粤ICP备20201119号-1
        <br/>
        粤公网安备 20201119号
    </p>
</div>
</body>
</html>